package com.jahe.learn.netty4.byteBuf;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;

import java.nio.charset.Charset;

/**
 * @author: jahe.lai
 * @create: 2020-01-06 16:47
 */
public class ByteBufTest {

    public static void main(String[] args) {

        copiedBuffer("Hello World");

        // 测试中文的处理(中文占三个字节) getCharSequence(...)
        copiedBuffer("赖Hello World");
    }

    private static void copiedBuffer(CharSequence string) {
        System.out.println("------------------" + string);
        ByteBuf byteBuf = Unpooled.copiedBuffer(string, Charset.forName("utf-8"));

        if (byteBuf.hasArray()) {

            System.out.println(new String(byteBuf.array()));

            //无中文 UnpooledByteBufAllocator$InstrumentedUnpooledUnsafeHeapByteBuf(ridx: 0, widx: 11, cap: 33)
            // Netty的ByteBuf支持自动扩容。
            // 有中文 UnpooledByteBufAllocator$InstrumentedUnpooledUnsafeHeapByteBuf(ridx: 0, widx: 14, cap: 36)
            System.out.println(byteBuf);
        }
    }
}
